package com.example.demo.security;

import cn.hutool.core.collection.ListUtil;
import com.example.demo.dao.UserRepository;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/***
 * <p>
 * Description: 加载用户信息处理
 * </p>
 * @author wangxiaoyuan
 * 2021年12月08日
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        if (user != null) {
            /*
            * 此处查询用户权限，方便后续接口权限校验
            * 若接口需要开启权限验证，则在对应接口上配置 @Secured("xxx")注解
            * */
            List<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("USER"));
            return new SecurityUser(user.getId(), user.getUsername(), user.getPassword(), authorities);
        }
        throw new UsernameNotFoundException("用户不存在");
    }
}
